package com.signway.utils;

import java.util.regex.Pattern;

/**
 * 网络工具类
 */
public class NetTools {
    private final static String TAG = "NetTools";

    public static Pattern ipPattern = Pattern
            .compile("^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$");

    public synchronized static byte[] ipToInt(String ip) {
        byte[] ipi = new byte[4];
        if (ip == null) {
            return ipi;
        }
        String[] ips = ip.split("\\.");
        ipi[0] = (byte) (Integer.parseInt(ips[0]) & 0xFF);
        ipi[1] = (byte) (Integer.parseInt(ips[1]) & 0xFF);
        ipi[2] = (byte) (Integer.parseInt(ips[2]) & 0xFF);
        ipi[3] = (byte) (Integer.parseInt(ips[3]) & 0xFF);
        return ipi;
    }

    public synchronized static String byteToIp(byte[] i) {
        if (i.length != 4) {
            return "255.255.255.255";
        } else {
            return String.valueOf(i[0] & 0xFF) + "."
                    + String.valueOf(i[1] & 0xFF) + "."
                    + String.valueOf(i[2] & 0xFF) + "."
                    + String.valueOf(i[3] & 0xFF);
        }
    }

    public synchronized static String intToIp(int paramInt) {
        return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "."
                + (0xFF & paramInt >> 24);
    }


}
